﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Business;
using AMM.Business.Config;
using AMM.Business.Produce;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.UserControls {
    public partial class ProducePlanEditControl : BaseEditControl {

        private bool _isNew;
        private List<ProducePlan> _producePlanList;

        public override object BaseEditModel {
            get {
                this._producePlanList.ForEach(x => x.ProduceConductId = Convert.ToInt32(this.ddlProduceConduct.SelectedValueEx));
                return this._producePlanList;
            }
            set { this._producePlanList = (List<ProducePlan>)value; }
        }

        public ProducePlanEditControl() {
            this.InitializeComponent();
            this.dgvEdit.AutoGenerateColumns = false;
        }

        private void ProducePlanEditControl_Load(object sender, EventArgs e) {
            this.BindProduceUnit();
            this.BindProduceConduct();

            this.BindDataGridView();
            this.CreateFormBindings();
            this.BindValidation();

            this._isNew = false;
        }

        private void BindDataGridView() {
            this.dgvEdit.DataSourceEx = this._producePlanList;
            if (!this._isNew && this._producePlanList != null && this._producePlanList.Count > 0) {
                this.ddlProduceConduct.Enabled = false;
                this.ddlProduceConduct.SelectedValueEx = this._producePlanList[0].ProduceConductId;
            }
        }

        private void BindProduceUnit() {
            this.ProduceUnit.DataSource = new BasicsProcessor().FetchEnabledBasicsByType(BasicsType.ProduceUnit);
            this.ProduceUnit.DisplayMember = "Name";
            this.ProduceUnit.ValueMember = "BasicsId";
        }

        private void BindProduceConduct() {
            ProduceConductSearchModel searchModel = new ProduceConductSearchModel();
            ProduceConductProcessor produceConductProcessor = new ProduceConductProcessor(int.MaxValue);
            List<ProduceConductViewModel> produceConductList = produceConductProcessor.FetchProduceConductViewModelList(searchModel).ToList();

            this.ddlProduceConduct.DisplayMemberEx = "ProduceConductCode";
            this.ddlProduceConduct.ValueMemberEx = "Key";
            this.ddlProduceConduct.HeightEx = 300;
            this.ddlProduceConduct.WidthEx = 400;
            this.ddlProduceConduct.PageSizeEx = 20;
            this.ddlProduceConduct.DataBind(new ProduceConductViewModel(), x => new {
                x.ProduceConductCode,
                x.CustomerOrderCode,
                x.ProductCode
            }, produceConductList);
        }

        private void CreateFormBindings() {
            /*
            this._producePlanList.CreateBinding(this.ddlProduceConduct, x => x.SelectedValueEx, y => y.ProduceConductId);
            this._fbs.CreateBinding(this.tbProducePlan, x => x.Text, y => y.Price);
            this._fbs.CreateBinding(this.tbProcurementCycle, x => x.Text, y => y.ProcurementCycle);
            this._fbs.CreateBinding(this.tbBatchAmount, x => x.Text, y => y.PreProcurementCycle);
            this._fbs.CreateBinding(this.cbIsMultiple, x => x.Checked, y => y.IsMultiple);
            this._fbs.CreateBinding(this.tbMinimumPackage, x => x.Text, y => y.MinimumPackage);
            this._fbs.CreateBinding(this.tbMinimumProcurement, x => x.Text, y => y.MinimumProcurement);
            this._fbs.CreateBinding(this.cmbProducePlanType, x => x.SelectedValue, y => y.UnitId);
            this._fbs.CreateBinding(this.ddlMaterial, x => x.SelectedValueEx, y => y.MaterialId);
            this._fbs.CreateBinding(this.attachment, x => x.Text, y => y.AttachmentId, DataSourceUpdateMode.OnPropertyChanged);
            */
        }

        private void BindValidation() {
            /*
            this.ValidatorForEdit.SetType(this.tbProducePlan, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbProducePlan, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbProducePlan, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbProducePlan, "单价只能由数字组成");

            this.ValidatorForEdit.SetType(this.ddlProduceConduct, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlProduceConduct, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlMaterial, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlMaterial, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbProducePlanType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbProducePlanType, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbProcurementCycle, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbProcurementCycle, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbProcurementCycle, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbProcurementCycle, "采购周期只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbBatchAmount, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbBatchAmount, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbBatchAmount, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbBatchAmount, "采购前置周期只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbMinimumPackage, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMinimumPackage, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMinimumPackage, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMinimumPackage, "最小包装只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbMinimumProcurement, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMinimumProcurement, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMinimumProcurement, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMinimumProcurement, "最低采购量只能由数字组成");
            */
        }

        private void ddlProduceConduct_ValueChanged(object sender, EventArgs e) {
            if (this.ddlProduceConduct.SelectedValueEx == null) { return; }
            int produceConductIdId = Convert.ToInt32(this.ddlProduceConduct.SelectedValueEx);
            ProduceConductViewModel pcv = (ProduceConductViewModel)this.ddlProduceConduct.SelectedItemEx;
            this.lblBatchAmount.Text = pcv.BatchAmount.ToString("G0");
            this.lblCustomerOrderCode.Text = pcv.CustomerOrderCode;
            this.lblProduceCode.Text = pcv.ProductCode;

            this._isNew = true;
            this._producePlanList = new ProducePlanProcessor().FetchProducePlanByProduceConductId(produceConductIdId);
            this.BindDataGridView();
        }
    }
}
